package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.modular.system.entity.ExamPlaceClass;
import cn.stylefeng.guns.modular.system.mapper.ExamPlaceClassMapper;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 考试地点-班级表 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-10
 */
@Service
public class ExamPlaceClassService extends ServiceImpl<ExamPlaceClassMapper,ExamPlaceClass> {

    public Map<String,Object> getClassNum(Long examPlaceId) {
        Map<String,Object> map = new HashMap<>();
        ExamPlaceClass placeClass = this.getOne(new QueryWrapper<ExamPlaceClass>()
                .eq("exam_place_id", examPlaceId)
                .gt("place_class_residue_num", 0)
                .eq("effective",1).orderByDesc("create_time"));
        if(placeClass == null){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        placeClass.setPlaceClassResidueNum(placeClass.getPlaceClassResidueNum()-1);
        this.updateById(placeClass);
        map.put("classNum",placeClass.getPlaceClassNum()-placeClass.getPlaceClassResidueNum());
        map.put("classId",placeClass.getExamPlaceClassId());
        return map;
    }
}
